package com.alex.server.service.impl;

import com.alex.server.domain.Category;
import com.alex.server.domain.Teacher;
import com.alex.server.domain.TeacherExample;
import com.alex.server.dto.CategoryDto;
import com.alex.server.dto.TeacherDto;
import com.alex.server.dto.PageDto;
import com.alex.server.mapper.TeacherMapper;
import com.alex.server.service.ITeacherService;
import com.alex.server.util.CopyUtil;
import com.alex.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional
public class TeacherServiceImpl implements ITeacherService {

    @Autowired
    private TeacherMapper mapper;

    @Override
    public PageDto list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        List<Teacher> teacherList = mapper.selectByExample(null);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teacherList);
        pageDto.setTotal(pageInfo.getTotal());
        List<TeacherDto> teacherDtoList = CopyUtil.copyList(teacherList, TeacherDto.class);
        pageDto.setList(teacherDtoList);
        return pageDto;
    }

    @Override
    public void save(TeacherDto teacherDto) {
        Teacher teacher = CopyUtil.copy(teacherDto, Teacher.class);
        if (StringUtils.isEmpty(teacherDto.getId())){
            this.insert(teacher);
        } else {
            this.update(teacher);
        }
    }

    @Override
    public void insert(Teacher teacher) {
        teacher.setId(UuidUtil.getShortUuid());
        mapper.insert(teacher);
    }

    @Override
    public void update(Teacher teacher) {
        mapper.updateByPrimaryKey(teacher);
    }

    @Override
    public void delete(String id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TeacherDto> all() {
        TeacherExample teacherExample = new TeacherExample();
        List<Teacher> teacherList = mapper.selectByExample(teacherExample);
        List<TeacherDto> teacherDtoList = CopyUtil.copyList(teacherList, TeacherDto.class);
        return teacherDtoList;
    }

}